<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>watcher</h2>
      <p>{{ a }}</p>
      <p>{{ b }}</p>
    </div>

    <script>
      const str = document.querySelector("#app").innerHTML;
      const data = {
        a: 3,
        b: 4,
      };

      const compile = (node) => {
        document.querySelector("#app").innerHTML = str;
        const children = [...node.children]; // 里面的每一个元素组成的数组
        const reg = /\{\{(.*)\}\}/;
        children.forEach((child) => {
          // 去查找里面有{{}}的元素，并且将data的属性替换进去
          if (reg.test(child.textContent)) {
            child.innerHTML = data[RegExp.$1.trim()];
          }
        });
      };
      compile(document.querySelector("#app"));

      const observer = (obj, key, value) => {
        Object.defineProperty(obj, key, {
          get() {
            console.log(key + "的值被获取了");
            return value;
          },
          set(val) {
            console.log(key + "的值从" + value + "变成了" + val);
            value = val;
            compile(document.querySelector("#app"));
          },
        });
      };
      Object.keys(data).forEach((item) => {
        observer(data, item, data[item]);
      });

      setTimeout(() => {
        data.a = 10;
      }, 2000);
      setTimeout(() => {
        data.b = 20;
      }, 4000);
    </script>
  </body>
</html>
